#include "ParserSoap.h"
#include <libxml/parser.h>  /*trabaja con los ficheros xml*/
#include <libxml/tree.h>    /*trabaja con el árbol generado de parsear el xml*/

ParserSoap::ParserSoap() {}

ParserSoap::~ParserSoap() {}

void ParserSoap::parsear_circuito(xmlNodePtr nodeXml,xmlNodePtr nodeSoap){
	char* name = (char*) xmlGetProp (nodeXml, (const xmlChar *)NOMBRE);
	char* nro_id = (char*) xmlGetProp (nodeXml, (const xmlChar *)NRO_ID_SOAP);
	if(name != NULL){
		xmlSetProp(nodeSoap,(xmlChar*)NOMBRE, (xmlChar*)name);
		xmlSetProp(nodeSoap,(xmlChar*)NRO_ID_SOAP, (xmlChar*)nro_id);
	}
}

void ParserSoap::parsear_compuertas(xmlNodePtr nodeXml,xmlNodePtr nodeSoap){
	xmlNodePtr nodeCompXml, nodeCompSoap;
	nodeCompXml = nodeXml->xmlChildrenNode;
	if(nodeCompXml != NULL){
		do{
			std::string name = (char*)nodeCompXml->name;
			if((name.compare(AND) == 0)  || (name.compare(OR) == 0)
							 || (name.compare(NOT) == 0) || (name.compare(XOR) == 0) ){

				nodeCompSoap= xmlNewChild(nodeSoap, NULL, (xmlChar*)nodeCompXml->name,NULL);

				xmlSetProp(nodeCompSoap,(xmlChar*)ID, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)ID));
				xmlSetProp(nodeCompSoap,(xmlChar*)TIME, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)TIME));
				xmlSetProp(nodeCompSoap,(xmlChar*)ENTRADA_1, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)ENTRADA_1));
				xmlSetProp(nodeCompSoap,(xmlChar*)ENTRADA_2, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)ENTRADA_2));
				xmlSetProp(nodeCompSoap,(xmlChar*)POS_X, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)POS_X));
				xmlSetProp(nodeCompSoap,(xmlChar*)POS_Y, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)POS_Y));
				xmlSetProp(nodeCompSoap,(xmlChar*)ANGULO, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)ANGULO));
			}
			nodeCompXml = nodeCompXml->next;
		}while(nodeCompXml);
	}
}
void ParserSoap::parsear_entradas(xmlNodePtr nodeXml,xmlNodePtr nodeSoap){
	xmlNodePtr nodeCompXml, nodeCompSoap;
	nodeCompXml = nodeXml->xmlChildrenNode;
	std::string name;
	if(nodeCompXml != NULL){
		do{
			name = (char*)nodeCompXml->name;

			if((name.compare(ENTR) == 0) ){
				nodeCompSoap= xmlNewChild(nodeSoap, NULL, (xmlChar*)nodeCompXml->name,NULL);

				xmlSetProp(nodeCompSoap,(xmlChar*)NOMBRE, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)NOMBRE));
				xmlSetProp(nodeCompSoap,(xmlChar*)ID, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)ID));
				xmlSetProp(nodeCompSoap,(xmlChar*)ESTADO, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)ESTADO));
				xmlSetProp(nodeCompSoap,(xmlChar*)POS_X, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)POS_X));
				xmlSetProp(nodeCompSoap,(xmlChar*)POS_Y, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)POS_Y));
				xmlSetProp(nodeCompSoap,(xmlChar*)ANGULO, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)ANGULO));
			}
			nodeCompXml = nodeCompXml->next;
		}while(nodeCompXml);
	}
}

void ParserSoap::parsear_salidas(xmlNodePtr nodeXml,xmlNodePtr nodeSoap){
	xmlNodePtr nodeCompXml, nodeCompSoap;
	nodeCompXml = nodeXml->xmlChildrenNode;
	std::string name;
	if(nodeCompXml != NULL){
		do{
			name = (char*)nodeCompXml->name;

			if((name.compare(SAL) == 0) ){
				nodeCompSoap= xmlNewChild(nodeSoap, NULL, (xmlChar*)nodeCompXml->name,NULL);

				xmlSetProp(nodeCompSoap,(xmlChar*)NOMBRE, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)NOMBRE));
				xmlSetProp(nodeCompSoap,(xmlChar*)ID, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)ID));
				xmlSetProp(nodeCompSoap,(xmlChar*)ENTRADA, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)ENTRADA));
				xmlSetProp(nodeCompSoap,(xmlChar*)POS_X, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)POS_X));
				xmlSetProp(nodeCompSoap,(xmlChar*)POS_Y, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)POS_Y));
				xmlSetProp(nodeCompSoap,(xmlChar*)ANGULO, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)ANGULO));
			}
			nodeCompXml = nodeCompXml->next;
		}while(nodeCompXml);
	}
}

void ParserSoap::parsear_conexiones(xmlNodePtr nodeXml,xmlNodePtr nodeSoap){
	xmlNodePtr nodeCompXml, nodeCompSoap;
	nodeCompXml = nodeXml->xmlChildrenNode;
	std::string name;
	if(nodeCompXml != NULL){
		do{
			name = (char*)nodeCompXml->name;

			if((name.compare(CONEX) == 0) ){

				nodeCompSoap= xmlNewChild(nodeSoap, NULL, (xmlChar*)nodeCompXml->name,NULL);

				xmlSetProp(nodeCompSoap,(xmlChar*)ID, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)ID));
				xmlSetProp(nodeCompSoap,(xmlChar*)NRO_ENTRADA, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)NRO_ENTRADA));
				xmlSetProp(nodeCompSoap,(xmlChar*)NRO_SALIDA, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)NRO_SALIDA));
				xmlSetProp(nodeCompSoap,(xmlChar*)EMISOR, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)EMISOR));
				xmlSetProp(nodeCompSoap,(xmlChar*)RECEPTOR, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)RECEPTOR));
			}
			nodeCompXml = nodeCompXml->next;
		}while(nodeCompXml);
	}
}

void ParserSoap::parsear_cables(xmlNodePtr nodeXml,xmlNodePtr nodeSoap){
	xmlNodePtr nodeCompXml, nodeCompSoap, nodeQuiebreSoap,nodeQuiebreXml;
	nodeCompXml = nodeXml->xmlChildrenNode;
	std::string name,nameQuiebre;
	if(nodeCompXml != NULL){
		do{
			name = (char*)nodeCompXml->name;

			if((name.compare(CABLE) == 0) ){
				nodeCompSoap= xmlNewChild(nodeSoap, NULL, (xmlChar*)nodeCompXml->name,NULL);

				xmlSetProp(nodeCompSoap,(xmlChar*)NRO_ID_SOAP, (xmlChar*) xmlGetProp (nodeCompXml, (const xmlChar *)NRO_ID_SOAP));
				nodeQuiebreXml = nodeCompXml->xmlChildrenNode;

				do{
					nameQuiebre = (char*)nodeQuiebreXml->name;

					if((nameQuiebre.compare(QUIEBRE) == 0) ){
						nodeQuiebreSoap = xmlNewChild(nodeCompSoap, NULL, (xmlChar*)nodeQuiebreXml->name,NULL);

						xmlSetProp(nodeQuiebreSoap,(xmlChar*)POS_X, (xmlChar*) xmlGetProp (nodeQuiebreXml, (const xmlChar *)POS_X));
						xmlSetProp(nodeQuiebreSoap,(xmlChar*)POS_Y, (xmlChar*) xmlGetProp (nodeQuiebreXml, (const xmlChar *)POS_Y));
					}
					nodeQuiebreXml = nodeQuiebreXml->next;
					}while(nodeQuiebreXml);
			}
			nodeCompXml = nodeCompXml->next;
		}while(nodeCompXml);
	}
}

void ParserSoap::parsear_elementos_externos(xmlNodePtr nodeXml,xmlNodePtr nodeSoap){
	xmlNodePtr nodeCompXml,nodeEntradaXml, nodeCompSoap, nodeDatosSoap,nodeElementoXml, nodeEntradasSoap,nodeListaEntradasSoap;

	nodeCompXml = nodeXml->xmlChildrenNode;
	std::string name,nameDatos, nameEntrada;
	if(nodeCompXml != NULL){
		do{
			name = (char*)nodeCompXml->name;

			if((name.compare(EXTERNO) == 0) ){
				nodeCompSoap= xmlNewChild(nodeSoap, NULL, (xmlChar*)nodeCompXml->name,NULL);

				nodeElementoXml = nodeCompXml->xmlChildrenNode;

				do{
					nameDatos = (char*)nodeElementoXml->name;

					if((nameDatos.compare(DATOS) == 0) ){
						nodeDatosSoap = xmlNewChild(nodeCompSoap, NULL, (xmlChar*)nodeElementoXml->name,NULL);

						xmlSetProp(nodeDatosSoap,(xmlChar*)NOMBRE, (xmlChar*) xmlGetProp (nodeElementoXml, (const xmlChar *)NOMBRE));
						xmlSetProp(nodeDatosSoap,(xmlChar*)ID, (xmlChar*) xmlGetProp (nodeElementoXml, (const xmlChar *)ID));
						xmlSetProp(nodeDatosSoap,(xmlChar*)TIME, (xmlChar*) xmlGetProp (nodeElementoXml, (const xmlChar *)TIME));
						xmlSetProp(nodeDatosSoap,(xmlChar*)PORT, (xmlChar*) xmlGetProp (nodeElementoXml, (const xmlChar *)PORT));
						xmlSetProp(nodeDatosSoap,(xmlChar*)IP, (xmlChar*) xmlGetProp (nodeElementoXml, (const xmlChar *)IP));
						xmlSetProp(nodeDatosSoap,(xmlChar*)CANT_ENT, (xmlChar*) xmlGetProp (nodeElementoXml, (const xmlChar *)CANT_ENT));
						xmlSetProp(nodeDatosSoap,(xmlChar*)CANT_SAL, (xmlChar*) xmlGetProp (nodeElementoXml, (const xmlChar *)CANT_SAL));
						xmlSetProp(nodeDatosSoap,(xmlChar*)POS_X, (xmlChar*) xmlGetProp (nodeElementoXml, (const xmlChar *)POS_X));
						xmlSetProp(nodeDatosSoap,(xmlChar*)POS_Y, (xmlChar*) xmlGetProp (nodeElementoXml, (const xmlChar *)POS_Y));
						xmlSetProp(nodeDatosSoap,(xmlChar*)ANGULO, (xmlChar*) xmlGetProp (nodeElementoXml, (const xmlChar *)ANGULO));
					}
					if((nameDatos.compare(ENTRADAS) == 0) ){
						nodeListaEntradasSoap = xmlNewChild(nodeCompSoap, NULL, (xmlChar*)nodeElementoXml->name,NULL);

						nodeEntradaXml = nodeElementoXml->xmlChildrenNode;
						do{
							nameEntrada = (char*)nodeEntradaXml->name;
							if((nameEntrada.compare(ENTR) == 0) ){
								nodeEntradasSoap = xmlNewChild(nodeListaEntradasSoap, NULL, (xmlChar*)nodeEntradaXml->name,NULL);
								xmlSetProp(nodeEntradasSoap,(xmlChar*)ENTRADA, (xmlChar*) xmlGetProp (nodeEntradaXml, (const xmlChar *)ENTRADA));
							}
							nodeEntradaXml = nodeEntradaXml->next;
							}while(nodeEntradaXml);
					}
					nodeElementoXml = nodeElementoXml->next;
					}while(nodeElementoXml);
			}
			nodeCompXml = nodeCompXml->next;
		}while(nodeCompXml);
	}
}
